package com.acwing.partition3;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/3/28 10:14
 */
public class AC286选课 {

    private static final int N = 310;
    private static int[] h = new int[N], e = new int[N], ne = new int[N], hw = new int[N];
    //f[i][j]表示以i为根节点，可以选j门课程可以获得到的最大学分
    private static int[][] f = new int[N][N];
    private static int n, m, idx = 0;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = nextInt(); m = nextInt();
        Arrays.fill(h, -1);
        //0作为超级源点，且不是任何子节点的依赖
        for (int i = 1; i <= n; i++) {
            int a = nextInt(), b = nextInt();
            add(a, i);
            hw[i] = b;
        }
        dfs(0);
        writer.println(f[0][m]);
        writer.flush();
    }

    private static void dfs(int u) {
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            dfs(v);
            //分别枚举当前节点可选的体积和子节点使用的体积
            for (int j = m; j >= 0; j--) {
                for (int k = 0; k <= j; k++) {
                    f[u][j] = Math.max(f[u][j], f[u][j - k] + f[v][k]);
                }
            }
        }
        //选择子节点的话当前节点必选
        if (u != 0) {
            for (int j = m; j >= 1; j--) f[u][j] = f[u][j - 1] + hw[u];
        }
    }

    private static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
